package com.derbysoft.dswitch.adapter.ihg.web.action.admin.requestlog;

import com.derbysoft.dswitch.adapter.ihg.common.domain.OperationType;
import com.derbysoft.dswitch.adapter.ihg.common.domain.RequestCode;
import com.derbysoft.dswitch.adapter.ihg.common.domain.ReservationLog;
import com.derbysoft.dswitch.adapter.ihg.common.domain.TransactionLog;
import com.derbysoft.dswitch.adapter.ihg.common.repository.*;
import com.derbysoft.dswitch.adapter.ihg.common.utils.DateTimeUtils;
import com.derbysoft.dswitch.adapter.ihg.web.action.DMXAbstractAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * User: jason
 * Date: 2013-01-28
 */
public class DMXAction extends DMXAbstractAction {
    private static final String DATE_FORMATTER = "yyyyMMdd";
    @Autowired
    private TransactionLogService transactionLogService;

    @Autowired
    private ReservationLogService reservationLogService;

    @Autowired
    private TransactionLogDetailService transactionLogDetailService;

    @Autowired
    private TransactionLogRepository transactionLogRepository;

    @Autowired
    private ReservationLogDetailRepository reservationLogDetailRepository;

    private DMXQueryCondition condition;

    @Action("dmxTransactionLog")
    public String dmxQuery() {
        try {
            if (fromAvailRequest()) {
                createTable();
                transactionLogService.paginater(condition, getPaginater());
                return populateSucceededJsonResult(dmxPaging(), "transactionDetails");
            }
            reservationLogService.paginater(condition, getPaginater());
            return populateSucceededJsonResult(dmxPaging(), "reservationDetails");
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxViewDetail")
    public String dmxViewDetail() {
        try {
            if (condition == null || StringUtils.isBlank(condition.getId())) {
                return populateSucceededJsonResult("");
            }
            if (availRequest()) {
                return queryAvailLogDetail();
            }
            return queryReservationLogDetail();
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    private boolean fromAvailRequest() {
        return condition == null
                || condition.getRequestCode() == null
                || condition.getRequestCode().equals(RequestCode.OTA_HotelAvailRQ);
    }

    private String queryReservationLogDetail() {
        ReservationLog reservationLog = reservationLogService.load(condition.getId());
        if (reservationLog == null) {
            return populateSucceededJsonResult("");
        }
        reservationLog.setReservationDetails(reservationLogDetailRepository.load(reservationLog));
        return populateSucceededJsonResult(reservationLog,"reservationLog");
    }

    private String queryAvailLogDetail() {
        Date date = condition.getLogDate() == null ? DateTimeUtils.today() : DateTimeUtils.parse(condition.getLogDate());
        TransactionLog transactionLog = transactionLogService.load(condition.getId(), date);
        if (transactionLog == null) {
            return populateSucceededJsonResult("");
        }
        transactionLog.setTransactionDetails(transactionLogDetailService.loadAll(condition.getId(), date));
        return populateSucceededJsonResult(transactionLog);
    }

    private boolean availRequest() {
        return condition.getOperationType() == null
                || condition.getOperationType() == OperationType.HOTEL_AVAIL
                || condition.getOperationType() == OperationType.HOTEL_MULTI_AVAIL;
    }

    /**
     * prevent the table was not generated yet
     */
    private void createTable() {
        if (condition == null || (StringUtils.isBlank(condition.getStartDate()) && StringUtils.isBlank(condition.getEndDate()))) {
            transactionLogRepository.createTable(DateTimeUtils.formatDate(DateTimeUtils.today(), DATE_FORMATTER));
            return;
        }
        String date;
        if (StringUtils.isNotBlank(condition.getStartDate()) && StringUtils.isNotBlank(condition.getEndDate())) {
            date = DateTimeUtils.formatDate(DateTimeUtils.parse(condition.getStartDate()), DATE_FORMATTER);
        } else if (StringUtils.isNotBlank(condition.getStartDate())) {
            date = DateTimeUtils.formatDate(DateTimeUtils.parse(condition.getStartDate()), DATE_FORMATTER);
        } else {
            date = DateTimeUtils.formatDate(DateTimeUtils.parse(condition.getEndDate()), DATE_FORMATTER);
        }
        transactionLogRepository.createTable(date);
    }

    public DMXQueryCondition getCondition() {
        return condition;
    }

    public void setCondition(DMXQueryCondition condition) {
        this.condition = condition;
    }
}
